package com.net.dao.impl;

import java.sql.SQLException;
import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import com.net.dao.SuperDao;
import com.net.dao.UserDao;
import com.net.model.User;
@Component("userDao")
public class UserDaoImpl extends SuperDao implements UserDao{

	public boolean delete(User u) {
		User cust = (User) this.getHibernateTemplate().load(User.class, u.getId());
		if(cust!=null){
			cust.setRoles(null);
			this.getHibernateTemplate().delete(cust);
			System.out.println("delete successfully");
			return true;
		}else
			return false;
	}

	public List<User> getUsers(final int currentPage, final int perPage, final String orderType ,final String orderName) {
		System.out.println("99999999999");
		HibernateCallback hc = new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				if(orderType==null||orderName==null){
				return session.createQuery("from User").setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}else{
					System.out.println("from User order by "+orderName+" "+ orderType);
					return session.createQuery("from User order by "+orderName+" "+ orderType).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}
			}
		};
		
		System.out.println(getHibernateTemplate().executeFind(hc));
		return (List<User>)getHibernateTemplate().execute(hc);
	}

	public User loadUserById(int id) {
		return (User) this.getHibernateTemplate().load(User.class, id);
	}

	public User loadUserByName(String name) {
		List<User> users = getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(User.class).add(Restrictions.eq("name", name)));
		if(users!=null&&users.size()>0){
			return users.get(0);
		}else
			return null;
	}

	public void save(User u) {
		
		getHibernateTemplate().save(u);
		System.out.println("保存后ID："+u.getId());
		
	}

	public void update(User user) {
		getHibernateTemplate().saveOrUpdate(user);
		
	}

	public List<User> getUsersByCondition(final int currentPage, final int perPage,
			final String condition) {
		HibernateCallback hc = new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String str = "from User u where " + condition;
				System.out.println(str);
				return session.createQuery(str).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
			}
			
		};
		return getHibernateTemplate().executeFind(hc);
	}

	public int getCount() {
		HibernateCallback hc = new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("from User").list();
			}
			
		};
		return getHibernateTemplate().executeFind(hc).size();
	}

}
